#include "loginform.h"
#include "ui_loginform.h"
#include "QDebug"

const unsigned char cryptoChar[] = {0x11, 0x13, 0x17, 0x19, 0x23, 0x29, 0x31, 0x37, 0x41, 0x43, 0x47, 0x53, 0x59, 0x61 };


LoginForm::LoginForm(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::LoginForm)
{
    ui->setupUi(this);



    ui->usernamefield->setFocus(Qt::OtherFocusReason);
    _settings = new QSettings(QSettings::defaultFormat(), QSettings::UserScope, "winhalive", "login");
    if(_settings->contains("application/username") && (_settings->contains("application/password"))){

        QString username =  _settings->value("application/username").toString();
        QByteArray password =  _settings->value("application/password").toByteArray();

        ui->usernamefield->setText(username);
        ui->passwordfield->setText(decryptPassword(password));
        ui->checkBox->setChecked(true);
    }
    QWebView* webview = new QWebView();
    f = new FoodFinder(webview);

    connect(f, SIGNAL(done(bool)), this, SLOT(displayTodayMenu(bool)));
}

//decypt password
QByteArray decryptPassword(QByteArray encPass){
    QByteArray roastedPass;
    for ( int i = 0; i < encPass.length(); i++ )
        roastedPass[i] = encPass.at(i) ^ cryptoChar[i];
    return roastedPass;
}

//Simple encryption of password to be saved,
//Qsettings may use textfile in some platforms.
QByteArray encryptPassword(QString passwd){
    QByteArray roastedPass;
    for ( int i = 0; i < passwd.length(); i++ )
        roastedPass[i] = passwd.at(i).unicode() ^ cryptoChar[i];
    return roastedPass;

}


void LoginForm::displayTodayMenu(bool thereIsFood){
    if(thereIsFood){
        QList< QPair<QString,float> >* todayFood = f->getFoodList();

        for(int i =0 ;i < todayFood->size(); i++){
            QPair<QString,float> fi = todayFood->at(i);
            QChar euro(8364);
            QString foodandprice = fi.first + " [ PRICE " + QString::number(fi.second)+  euro + "  ]";
            QListWidgetItem *item = new QListWidgetItem(foodandprice,ui->listWidget,QListWidgetItem::Type);
            item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
            ui->listWidget->addItem (item);
        }
        ui->listWidget->setAlternatingRowColors (true);


    }
    else{
        QListWidgetItem *item = new QListWidgetItem("No Food Today [Perhaps it's a weekend ]", ui->listWidget,QListWidgetItem::Type);
        item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
        ui->listWidget->addItem (item);
    }

}
LoginForm::~LoginForm()
{
    delete ui;
}

void LoginForm::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//function called when user clickes login
void LoginForm::on_pushButton_clicked()
{
    QString uname = ui->usernamefield->text();
    QString pword = ui->passwordfield->text();

    //if checkbos is checked save the username and the encrypted password
    if(ui->checkBox->isChecked() ){

        _settings->setValue("application/username",uname);
        _settings->setValue("application/password",encryptPassword( pword));

    }

    emit loginDone(uname,pword);



}

void LoginForm::on_passwordfield_returnPressed()
{
    on_pushButton_clicked();

}


